<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: dark)"><meta name="generator" content="Hexo 5.4.2">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" integrity="sha256-DfWjNxDkM94fVBWx1H5BMMp0Zq7luBlV8QRcSES7s+0=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"mathieulin.gitee.io","root":"/","images":"/images","scheme":"Gemini","darkmode":true,"version":"8.12.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"manual","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="TodoListTodoList Vue前端项目TodoList Django后端项目1. 建立数据表数据库的配置文件在主目录下 djangoProject\settings.py中 数据库可视化工具直接建表  users表存储用户id，密码  todolist表，由于mysql数据库无法存储json格式的文件，因此我们将用户的todo放到多行数据中显示，每一行包括todo的id（主键），用户id">
<meta property="og:type" content="article">
<meta property="og:title" content="TodoList">
<meta property="og:url" content="https://mathieulin.gitee.io/2022/05/31/TodoList/index.html">
<meta property="og:site_name" content="Mathieu&#39;s blog">
<meta property="og:description" content="TodoListTodoList Vue前端项目TodoList Django后端项目1. 建立数据表数据库的配置文件在主目录下 djangoProject\settings.py中 数据库可视化工具直接建表  users表存储用户id，密码  todolist表，由于mysql数据库无法存储json格式的文件，因此我们将用户的todo放到多行数据中显示，每一行包括todo的id（主键），用户id">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://mathieulin.gitee.io/.io//1.jpg">
<meta property="article:published_time" content="2022-05-31T08:00:00.000Z">
<meta property="article:modified_time" content="2022-06-02T12:05:21.631Z">
<meta property="article:author" content="Mathieu Lin">
<meta property="article:tag" content="lzh">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://mathieulin.gitee.io/.io//1.jpg">


<link rel="canonical" href="https://mathieulin.gitee.io/2022/05/31/TodoList/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://mathieulin.gitee.io/2022/05/31/TodoList/","path":"2022/05/31/TodoList/","title":"TodoList"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>TodoList | Mathieu's blog</title>
  




<link rel="dns-prefetch" href="https://blog-commeny-mh5e4d5jo-mathieulin.vercel.app">
  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<link rel="alternate" href="/atom.xml" title="Mathieu's blog" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">Mathieu's blog</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">lzhow的debug路程</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li><li class="menu-item menu-item-schedule"><a href="/schedule/" rel="section"><i class="fa fa-calendar fa-fw"></i>日程表</a></li><li class="menu-item menu-item-sitemap"><a href="/sitemap.xml" rel="section"><i class="fa fa-sitemap fa-fw"></i>站点地图</a></li><li class="menu-item menu-item-commonweal"><a href="/404/" rel="section"><i class="fa fa-heartbeat fa-fw"></i>公益 404</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#TodoList"><span class="nav-number">1.</span> <span class="nav-text">TodoList</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#TodoList-Vue%E5%89%8D%E7%AB%AF%E9%A1%B9%E7%9B%AE"><span class="nav-number">1.1.</span> <span class="nav-text">TodoList Vue前端项目</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#TodoList-Django%E5%90%8E%E7%AB%AF%E9%A1%B9%E7%9B%AE"><span class="nav-number">1.2.</span> <span class="nav-text">TodoList Django后端项目</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E5%BB%BA%E7%AB%8B%E6%95%B0%E6%8D%AE%E8%A1%A8"><span class="nav-number">1.2.1.</span> <span class="nav-text">1. 建立数据表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E5%B0%86%E6%95%B0%E6%8D%AE%E8%A1%A8%E5%AF%BC%E5%87%BA%E4%B8%BA%E6%A8%A1%E5%9E%8B"><span class="nav-number">1.2.2.</span> <span class="nav-text">2. 将数据表导出为模型</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%89%8D%E5%90%8E%E7%AB%AF"><span class="nav-number">1.3.</span> <span class="nav-text">前后端</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%B8%80%E5%A4%A7%E6%81%B6%E5%BF%83%E4%B9%8B%E5%A4%84%E5%B0%B1%E6%98%AF%E8%B7%A8%E5%9F%9F%E8%AE%BF%E9%97%AE%EF%BC%8C%E6%88%91%E6%98%AF%E8%BF%99%E4%B9%88%E8%A7%A3%E5%86%B3%E7%9A%84"><span class="nav-number">1.3.0.1.</span> <span class="nav-text">一大恶心之处就是跨域访问，我是这么解决的</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BA%A4%E4%BA%92"><span class="nav-number">1.3.0.2.</span> <span class="nav-text">交互</span></a></li></ol></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">Mathieu Lin</p>
  <div class="site-description" itemprop="description">这是一个记载了lzhow传奇的一生的博客</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">14</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">分类</span>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">9</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/mathieulin" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;mathieulin" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:19241032@buaa.edu.cn" title="E-Mail → mailto:19241032@buaa.edu.cn" rel="noopener" target="_blank"><i class="fab fa-envelope fa-fw"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="http://121.199.35.110/" title="Todo → http:&#x2F;&#x2F;121.199.35.110&#x2F;" rel="noopener" target="_blank">Todo</a>
      </span>
      <span class="links-of-author-item">
        <a href="http://noietch.cn:88/" title="House → http:&#x2F;&#x2F;noietch.cn:88&#x2F;" rel="noopener" target="_blank">House</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-05d97c93-6101-4c53-8659-f5cf79396b93/0cdfad7c-f758-49ca-becf-6483b8fe95d5.apk" title="Douzero → https:&#x2F;&#x2F;vkceyugu.cdn.bspapp.com&#x2F;VKCEYUGU-05d97c93-6101-4c53-8659-f5cf79396b93&#x2F;0cdfad7c-f758-49ca-becf-6483b8fe95d5.apk" rel="noopener" target="_blank">Douzero</a>
      </span>
      <span class="links-of-author-item">
        <a href="/atom.xml" title="rss → &#x2F;atom.xml"><i class="fa fa-rss fa-fw"></i>rss</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://mathieulin.github.io/myblog" title="Another-blog → https:&#x2F;&#x2F;mathieulin.github.io&#x2F;myblog" rel="noopener" target="_blank">Another-blog</a>
      </span>
  </div>



        </div>
      </div>
    </div>
    <iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="//music.163.com/outchain/player?type=2&id=1951671851&auto=1&height=66"></iframe>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://mathieulin.gitee.io/2022/05/31/TodoList/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Mathieu Lin">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Mathieu's blog">
      <meta itemprop="description" content="这是一个记载了lzhow传奇的一生的博客">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="TodoList | Mathieu's blog">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          TodoList
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-05-31 16:00:00" itemprop="dateCreated datePublished" datetime="2022-05-31T16:00:00+08:00">2022-05-31</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2022-06-02 20:05:21" itemprop="dateModified" datetime="2022-06-02T20:05:21+08:00">2022-06-02</time>
    </span>

  
  
  <span class="post-meta-item">
    
    <span class="post-meta-item-icon">
      <i class="far fa-comment"></i>
    </span>
    <span class="post-meta-item-text">评论数：</span>
  
    <a title="waline" href="/2022/05/31/TodoList/#waline" itemprop="discussionUrl">
      <span class="post-comments-count waline-comment-count" data-path="/2022/05/31/TodoList/" itemprop="commentCount"></span>
    </a>
  </span>
  
  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="TodoList"><a href="#TodoList" class="headerlink" title="TodoList"></a>TodoList</h1><h2 id="TodoList-Vue前端项目"><a href="#TodoList-Vue前端项目" class="headerlink" title="TodoList Vue前端项目"></a>TodoList Vue前端项目</h2><h2 id="TodoList-Django后端项目"><a href="#TodoList-Django后端项目" class="headerlink" title="TodoList Django后端项目"></a>TodoList Django后端项目</h2><h3 id="1-建立数据表"><a href="#1-建立数据表" class="headerlink" title="1. 建立数据表"></a>1. 建立数据表</h3><p>数据库的配置文件在主目录下</p>
<p>djangoProject\settings.py中</p>
<p>数据库可视化工具直接建表</p>
<ul>
<li><p>users表存储用户id，密码</p>
</li>
<li><p>todolist表，由于mysql数据库无法存储json格式的文件，因此我们将用户的todo放到多行数据中显示，每一行包括todo的id（主键），用户id（外键），是否完成， 是否可删除， 标题， 开始时间，结束时间</p>
</li>
</ul>
<h3 id="2-将数据表导出为模型"><a href="#2-将数据表导出为模型" class="headerlink" title="2. 将数据表导出为模型"></a>2. 将数据表导出为模型</h3><p>我这里用的是conda环境，所以需要在命令行中执行</p>
<p>首页，激活环境</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">conda activate django</span><br></pre></td></tr></table></figure>

<p>然后我们建立一个app，我们就叫做TodoList了</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">django-admin.py startapp TodoList</span><br><span class="line"># 将数据库的表的模型用inspectdb命令转换</span><br><span class="line">python manage.py inspectdb &gt; TodoList/models.py</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>然后我们在settings.py中注册这个TodoList</p>
<p><img src="/.io//1.jpg"></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">python manage.py makemigrations</span><br><span class="line">python manage.py migrate</span><br></pre></td></tr></table></figure>





<h2 id="前后端"><a href="#前后端" class="headerlink" title="前后端"></a>前后端</h2><h4 id="一大恶心之处就是跨域访问，我是这么解决的"><a href="#一大恶心之处就是跨域访问，我是这么解决的" class="headerlink" title="一大恶心之处就是跨域访问，我是这么解决的"></a>一大恶心之处就是跨域访问，我是这么解决的</h4><p>前端项目中的main.js</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// main.js</span></span><br><span class="line"><span class="keyword">import</span> axios <span class="keyword">from</span> <span class="string">&#x27;axios&#x27;</span></span><br><span class="line"><span class="title class_">Vue</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">$axios</span> = axios</span><br><span class="line">axios.<span class="property">defaults</span>.<span class="property">baseURL</span> = <span class="string">&#x27;http://127.0.0.1:8000/api/todo&#x27;</span>;</span><br><span class="line">axios.<span class="property">defaults</span>.<span class="property">headers</span>.<span class="property">post</span>[<span class="string">&#x27;Content-Type&#x27;</span>] = <span class="string">&#x27;application/x-www-fromurlencodeed&#x27;</span></span><br><span class="line">axios.<span class="property">defaults</span>.<span class="property">headers</span>[<span class="string">&#x27;Content-Type&#x27;</span>]=<span class="string">&#x27;application/x-www-form-urlencoded&#x27;</span>;</span><br></pre></td></tr></table></figure>

<p>后端项目中</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">allowed_origins = [<span class="string">&#x27;http://127.0.0.1:8000&#x27;</span>]</span><br><span class="line">origin = request.headers[<span class="string">&#x27;Origin&#x27;</span>]</span><br><span class="line"></span><br><span class="line">response = JsonResponse(&#123;<span class="string">&#x27;errno&#x27;</span>: <span class="number">1001</span>, <span class="string">&#x27;msg&#x27;</span>: <span class="string">&quot;请求方式错误&quot;</span>&#125;)</span><br><span class="line">response[<span class="string">&quot;Access-Control-Allow-Origin&quot;</span>] = origin</span><br><span class="line"><span class="keyword">return</span> response</span><br></pre></td></tr></table></figure>

<p>挺麻烦的，整了好久。好像有工具包可以来跨域访问，但我pip安装出问题了</p>
<h4 id="交互"><a href="#交互" class="headerlink" title="交互"></a>交互</h4><p>我这里一般是前端传json格式</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">this</span>.$axios(&#123;</span><br><span class="line">      <span class="attr">method</span>: <span class="string">&#x27;POST&#x27;</span>,              <span class="comment">/* 指明请求方式，可以是 get 或 post */</span></span><br><span class="line">      <span class="attr">url</span>: <span class="string">&#x27;/loadTodo&#x27;</span>,      <span class="comment">/* 指明后端 api 路径，由于在 main.js 已指定根路径，因此在此处只需写相对路由 */</span></span><br><span class="line">      <span class="attr">data</span>: qs.<span class="title function_">stringify</span>(&#123;        <span class="comment">/* 需要向后端传输的数据，此处使用 qs.stringify 将 json 数据序列化以发送后端 */</span></span><br><span class="line">          <span class="attr">uid</span>:<span class="variable language_">this</span>.<span class="property">$store</span>.<span class="property">state</span>.<span class="property">user</span>.<span class="property">username</span></span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;)</span><br><span class="line">    .<span class="title function_">then</span>(<span class="function"><span class="params">result</span> =&gt;</span> &#123;                  <span class="comment">/* res 是 response 的缩写 */</span></span><br><span class="line">      <span class="keyword">if</span> (result.<span class="property">data</span>.<span class="property">errno</span> === <span class="number">0</span>)       <span class="comment">/* res.data 是后端返回的数据对象 */</span></span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(result.<span class="property">data</span>.<span class="property">res</span>);</span><br><span class="line">      <span class="keyword">else</span></span><br><span class="line">        <span class="title function_">alert</span>(<span class="string">&quot;获取失败！&quot;</span>);</span><br><span class="line">    &#125;)</span><br><span class="line">    .<span class="title function_">catch</span>(<span class="function"><span class="params">err</span> =&gt;</span> &#123;                 <span class="comment">/* 请求若出现路由找不到等其它异常，则在终端输出错误信息 */</span></span><br><span class="line">      <span class="variable language_">console</span>.<span class="title function_">log</span>(err);</span><br><span class="line">    &#125;)</span><br></pre></td></tr></table></figure>

<p>然后后端</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@csrf_exempt</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">loadTodo</span>(<span class="params">request</span>):</span><br><span class="line">    allowed_origins = [<span class="string">&#x27;http://127.0.0.1:8000&#x27;</span>]</span><br><span class="line">    origin = request.headers[<span class="string">&#x27;Origin&#x27;</span>]</span><br><span class="line">    <span class="keyword">if</span> request.method == <span class="string">&#x27;POST&#x27;</span>:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;i am here&quot;</span>)</span><br><span class="line">        uid = request.POST.get(<span class="string">&#x27;uid&#x27;</span>)</span><br><span class="line">        <span class="built_in">print</span>(uid)</span><br><span class="line">        <span class="comment"># print(request.session[&#x27;uid&#x27;])</span></span><br><span class="line">        todos = Todolist.objects.<span class="built_in">all</span>()</span><br><span class="line">        res = []</span><br><span class="line">        <span class="keyword">for</span> todo <span class="keyword">in</span> todos:</span><br><span class="line">            <span class="keyword">if</span> todo.uid == uid:</span><br><span class="line">                todoJson = &#123;<span class="string">&quot;uid&quot;</span>: todo.uid, <span class="string">&quot;id&quot;</span>: todo.<span class="built_in">id</span>, <span class="string">&quot;title&quot;</span>: todo.title,</span><br><span class="line">                            <span class="string">&quot;isDone&quot;</span>: todo.isdone, <span class="string">&quot;operated&quot;</span>: todo.operated,</span><br><span class="line">                            <span class="string">&quot;date_begin&quot;</span>: todo.date_begin, <span class="string">&quot;date_end&quot;</span>: todo.date_end&#125;</span><br><span class="line">                res.append(todoJson)</span><br><span class="line">        res = &#123;<span class="string">&quot;res&quot;</span>: res&#125;</span><br><span class="line">        response = JsonResponse(&#123;<span class="string">&#x27;errno&#x27;</span>: <span class="number">0</span>, <span class="string">&#x27;data&#x27;</span>: res&#125;)</span><br><span class="line">        response[<span class="string">&quot;Access-Control-Allow-Origin&quot;</span>] = origin</span><br><span class="line">        <span class="keyword">return</span> response</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        response = JsonResponse(&#123;<span class="string">&#x27;errno&#x27;</span>: <span class="number">1001</span>, <span class="string">&#x27;msg&#x27;</span>: <span class="string">&quot;请求方式错误&quot;</span>&#125;)</span><br><span class="line">        response[<span class="string">&quot;Access-Control-Allow-Origin&quot;</span>] = origin</span><br><span class="line">        <span class="keyword">return</span> response</span><br></pre></td></tr></table></figure>



<p>使用request.POST.get来获取值，然后将response传回前端</p>
<p>前端result.data.data可以接收传回来的信息</p>

    </div>

    
    
    

    <footer class="post-footer"><script src="//sdk.jinrishici.com/v2/browser/jinrishici.js"></script>
<script>
  jinrishici.load((result) => {
    let jrsc = document.getElementById('jrsc');
    const data = result.data;
    let author = data.origin.author;
    let title = '《' + data.origin.title + '》';
    let content = data.content.substr(0, data.content.length - 1);
    let dynasty = data.origin.dynasty.substr(0, data.origin.dynasty.length - 1);
    jrsc.innerText = content + ' @ ' + dynasty + '·' + author + title;
  });
</script>
<div style="text-align: center"><span id="jrsc" >正在加载今日诗词....</span></div>


        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2022/05/31/java%E4%BD%9C%E4%B8%9A%E6%B3%A8%E6%84%8F/" rel="prev" title="仿抖音项目开发遇到的问题">
                  <i class="fa fa-chevron-left"></i> 仿抖音项目开发遇到的问题
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2022/06/01/%E5%90%8E%E7%AB%AF%E9%83%A8%E7%BD%B2/" rel="next" title="后端项目傻逼式部署">
                  后端项目傻逼式部署 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments" id="waline"></div>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Mathieu Lin</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div><script color="0,0,255" opacity="0.5" zIndex="-1" count="99" src="https://cdnjs.sourcegcdn.com//ajax/libs/canvas-nest.js/1.0.1/canvas-nest.js"></script>

    </div>
  </footer>

  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.5.0/pjax.min.js" integrity="sha256-3NkoLDrmHLTYj7csHIZSr0MHAFTXth7Ua/DDt4MRUAg=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  
<script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.0/search.js" integrity="sha256-vXZMYLEqsROAXkEw93GGIvaB2ab+QW6w3+1ahD9nXXA=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>





  




<script class="next-config" data-name="waline" type="application/json">{"lang":"zh-CN","enable":true,"serverURL":"https://blog-commeny-mh5e4d5jo-mathieulin.vercel.app","cssUrl":"https://unpkg.com/@waline/client@v2/dist/waline.css","commentCount":true,"pageview":false,"el":"#waline","comment":true,"libUrl":"//unpkg.com/@waline/client@v2/dist/waline.js","path":"/2022/05/31/TodoList/"}</script>
<link rel="stylesheet" href="https://unpkg.com/@waline/client@v2/dist/waline.css">
<script>
document.addEventListener('page:loaded', () => {
  NexT.utils.loadComments(CONFIG.waline.el).then(() =>
    NexT.utils.getScript(CONFIG.waline.libUrl, { condition: window.Waline })
  ).then(() => 
    Waline.init(Object.assign({}, CONFIG.waline,{ el: document.querySelector(CONFIG.waline.el) }))
  );
});
</script>

</body>
</html>

<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/src/clicklove.js"></script>
